{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1bc2016-4af1-4967-97d1-80c64346e38b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "== Status ==<br>Current time: 2023-01-31 15:11:14 (running for 00:00:03.04)<br>Memory usage on this node: 48.9/251.6 GiB<br>Using FIFO scheduling algorithm.<br>Resources requested: 2.0/12 CPUs, 1.0/1 GPUs, 0.0/18.59 GiB heap, 0.0/9.3 GiB objects<br>Result logdir: /root/ray_results/CNNTrain_2023-01-31_15-11-11<br>Number of trials: 16/20 (15 PENDING, 1 RUNNING)<br><table>\n",
       "<thead>\n",
       "<tr><th>Trial name          </th><th>status  </th><th>loc             </th><th style=\"text-align: right;\">  linear1</th><th style=\"text-align: right;\">  linear2</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>CNNTrain_71142_00000</td><td>RUNNING </td><td>172.17.0.11:5928</td><td style=\"text-align: right;\">     2500</td><td style=\"text-align: right;\">     2000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00001</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3500</td><td style=\"text-align: right;\">     2000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00002</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3000</td><td style=\"text-align: right;\">     1500</td></tr>\n",
       "<tr><td>CNNTrain_71142_00003</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     2500</td><td style=\"text-align: right;\">     1500</td></tr>\n",
       "<tr><td>CNNTrain_71142_00004</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     2500</td><td style=\"text-align: right;\">     2000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00005</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3500</td><td style=\"text-align: right;\">     1500</td></tr>\n",
       "<tr><td>CNNTrain_71142_00006</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3500</td><td style=\"text-align: right;\">     1000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00007</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     2500</td><td style=\"text-align: right;\">     2000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00008</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3500</td><td style=\"text-align: right;\">     2000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00009</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3000</td><td style=\"text-align: right;\">     1000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00010</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3000</td><td style=\"text-align: right;\">     1500</td></tr>\n",
       "<tr><td>CNNTrain_71142_00011</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3000</td><td style=\"text-align: right;\">     1000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00012</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3500</td><td style=\"text-align: right;\">     1500</td></tr>\n",
       "<tr><td>CNNTrain_71142_00013</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     3500</td><td style=\"text-align: right;\">     1000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00014</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     2500</td><td style=\"text-align: right;\">     1000</td></tr>\n",
       "<tr><td>CNNTrain_71142_00015</td><td>PENDING </td><td>                </td><td style=\"text-align: right;\">     2500</td><td style=\"text-align: right;\">     2000</td></tr>\n",
       "</tbody>\n",
       "</table><br><br>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m 2023-01-31 15:11:14,787\tERROR function_trainable.py:298 -- Runner Thread raised error.\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m Traceback (most recent call last):\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m   File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/function_trainable.py\", line 289, in run\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m     self._entrypoint()\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m   File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/function_trainable.py\", line 362, in entrypoint\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m     return self._trainable_func(\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m   File \"/root/miniconda3/lib/python3.8/site-packages/ray/util/tracing/tracing_helper.py\", line 466, in _resume_span\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m     return method(self, *_args, **_kwargs)\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m   File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/function_trainable.py\", line 684, in _trainable_func\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m     output = fn()\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m   File \"/tmp/ipykernel_466/1487508134.py\", line 95, in CNNTrain\n",
      "\u001B[2m\u001B[36m(CNNTrain pid=5928)\u001B[0m UnboundLocalError: local variable 'model' referenced before assignment\n",
      "2023-01-31 15:11:14,833\tERROR trial_runner.py:987 -- Trial CNNTrain_71142_00000: Error processing event.\n",
      "ray.exceptions.RayTaskError(UnboundLocalError): \u001B[36mray::ImplicitFunc.train()\u001B[39m (pid=5928, ip=172.17.0.11, repr=CNNTrain)\n",
      "  File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/trainable.py\", line 349, in train\n",
      "    result = self.step()\n",
      "  File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/function_trainable.py\", line 417, in step\n",
      "    self._report_thread_runner_error(block=True)\n",
      "  File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/function_trainable.py\", line 589, in _report_thread_runner_error\n",
      "    raise e\n",
      "  File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/function_trainable.py\", line 289, in run\n",
      "    self._entrypoint()\n",
      "  File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/function_trainable.py\", line 362, in entrypoint\n",
      "    return self._trainable_func(\n",
      "  File \"/root/miniconda3/lib/python3.8/site-packages/ray/tune/trainable/function_trainable.py\", line 684, in _trainable_func\n",
      "    output = fn()\n",
      "  File \"/tmp/ipykernel_466/1487508134.py\", line 95, in CNNTrain\n",
      "UnboundLocalError: local variable 'model' referenced before assignment\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Result for CNNTrain_71142_00000:\n",
      "  date: 2023-01-31_15-11-14\n",
      "  experiment_id: e2442cd69f134a899a58df8960bdec64\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 5928\n",
      "  timestamp: 1675149074\n",
      "  trial_id: '71142_00000'\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.autograd import Variable\n",
    "from torch.utils.data import Dataset\n",
    "import torchvision\n",
    "import torch.nn.functional as F\n",
    "from sklearn.preprocessing import scale,MinMaxScaler,Normalizer,StandardScaler\n",
    "import torch.optim as optim\n",
    "from Regression.CnnModel import ConvNet, DeepSpectra, AlexNet\n",
    "import os\n",
    "from datetime import datetime\n",
    "from Evaluate.RgsEvaluate import ModelRgsevaluate, ModelRgsevaluatePro\n",
    "import matplotlib.pyplot  as plt\n",
    "from ray import tune\n",
    "from DataLoad.DataLoad import LoadNirtest\n",
    "LR = 0.001\n",
    "BATCH_SIZE = 16\n",
    "TBATCH_SIZE = 240\n",
    "\n",
    "\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "#自定义加载数据集\n",
    "class MyDataset(Dataset):\n",
    "    def __init__(self,specs,labels):\n",
    "        self.specs = specs\n",
    "        self.labels = labels\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        spec,target = self.specs[index],self.labels[index]\n",
    "        return spec,target\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.specs)\n",
    "\n",
    "\n",
    "\n",
    "###定义是否需要标准化\n",
    "def ZspPocessnew(X_train, X_test, y_train, y_test, need=True): #True:需要标准化，Flase：不需要标准化\n",
    "\n",
    "    global standscale\n",
    "    global yscaler\n",
    "\n",
    "    if (need == True):\n",
    "        standscale = StandardScaler()\n",
    "        X_train_Nom = standscale.fit_transform(X_train)\n",
    "        X_test_Nom = standscale.transform(X_test)\n",
    "\n",
    "        #yscaler = StandardScaler()\n",
    "        yscaler = MinMaxScaler()\n",
    "        y_train = yscaler.fit_transform(y_train.reshape(-1, 1))\n",
    "        y_test = yscaler.transform(y_test.reshape(-1, 1))\n",
    "\n",
    "        X_train_Nom = X_train_Nom[:, np.newaxis, :]\n",
    "        X_test_Nom = X_test_Nom[:, np.newaxis, :]\n",
    "\n",
    "        ##使用loader加载测试数据\n",
    "        data_train = MyDataset(X_train_Nom, y_train)\n",
    "        data_test = MyDataset(X_test_Nom, y_test)\n",
    "        return data_train, data_test\n",
    "    elif((need == False)):\n",
    "        yscaler = StandardScaler()\n",
    "        # yscaler = MinMaxScaler()\n",
    "\n",
    "        X_train_new = X_train[:, np.newaxis, :]  #\n",
    "        X_test_new = X_test[:, np.newaxis, :]\n",
    "\n",
    "        y_train = yscaler.fit_transform(y_train)\n",
    "        y_test = yscaler.transform(y_test)\n",
    "\n",
    "        data_train = MyDataset(X_train_new, y_train)\n",
    "        ##使用loader加载测试数据\n",
    "        data_test = MyDataset(X_test_new, y_test)\n",
    "\n",
    "        return data_train, data_test\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "def CNNTrain(config):\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    EPOCH=20\n",
    "\n",
    "\n",
    "    model1 = AlexNet(1, config['linear1'], config['linear2'], 3840)\n",
    "    device = \"gpu\"\n",
    "    if torch.cuda.is_available():\n",
    "        device = \"cuda:0\"\n",
    "        if torch.cuda.device_count() > 1:\n",
    "            model = nn.DataParallel(model1)\n",
    "    model.to(device)\n",
    "\n",
    "\n",
    "    \n",
    "\n",
    "    criterion = nn.MSELoss().to(device)  # 损失函数为焦损函数，多用于类别不平衡的多分类问题\n",
    "    optimizer = optim.Adam(model.parameters(), lr=LR)#,  weight_decay=0.001)  # 优化方式为mini-batch momentum-SGD，并采用L2正则化（权重衰减）\n",
    "    # # initialize the early_stopping object\n",
    "    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor=0.5, verbose=1, eps=1e-06,\n",
    "                                                           patience=20)\n",
    "    print(\"Start Training!\")  # 定义遍历数据集的次数\n",
    "    # to track the training loss as the model trains\n",
    "    for epoch in range(EPOCH):\n",
    "        train_losses = []\n",
    "#         model.train()  # 不训练\n",
    "        train_rmse = []\n",
    "        train_r2 = []\n",
    "        train_mae = []\n",
    "        for i, data in enumerate(train_loader):  # gives batch data, normalize x when iterate train_loader\n",
    "            inputs, labels = LoadNirtest('Rgs')     # 输入和标签都等于data\n",
    "            inputs = Variable(inputs).type(torch.FloatTensor).to(device)  # batch x\n",
    "            #print(inputs.shape)\n",
    "            labels = Variable(labels).type(torch.FloatTensor).to(device)  # batch y\n",
    "            #print(labels.shape)\n",
    "            output = model(inputs)  # cnn output\n",
    "            loss = criterion(output, labels)  # MSE\n",
    "            optimizer.zero_grad()  # clear gradients for this training step\n",
    "            loss.backward()  # backpropagation, compute gradients\n",
    "            optimizer.step()  # apply gradients\n",
    "            pred = output.detach().cpu().numpy()\n",
    "            y_true = labels.detach().cpu().numpy()\n",
    "            train_losses.append(loss.item())\n",
    "            rmse, R2, mae = ModelRgsevaluatePro(pred, y_true, yscaler)\n",
    "            # plotpred(pred, y_true, yscaler))\n",
    "            train_rmse.append(rmse)\n",
    "            train_r2.append(R2)\n",
    "            train_mae.append(mae)\n",
    "        avg_train_loss = np.mean(train_losses)\n",
    "        avgrmse = np.mean(train_rmse)\n",
    "        avgr2 = np.mean(train_r2)\n",
    "        avgmae = np.mean(train_mae)\n",
    "        tune.report(my_loss=avg_train_loss)\n",
    "\n",
    "if __name__ == '__main__':\n",
    " \n",
    "    config = {\n",
    "        \"linear1\": tune.choice([3500,3000,2500]),  # 自定义采样\n",
    "        \"linear2\": tune.choice([1000,1500,2000]),  # 从给定值中随机选择\n",
    "    }\n",
    "    result = tune.run(  # 执行训练过程，执行到这里就会根据config自动调参了\n",
    "        CNNTrain,  # 要训练的模型\n",
    "        resources_per_trial={\"cpu\": 2, \"gpu\": 1},# 指定训练资源\n",
    "        config=config,\n",
    "        num_samples=20,  # 迭代的次数\n",
    "    )\n",
    "    # 得到最后的结果\n",
    "    print(\"======================== Result =========================\")\n",
    "    print(result.results_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c03b62c7-df84-4b7a-b653-01329c490c7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-01-31 15:06:27,392\tWARNING utils.py:594 -- Detecting docker specified CPUs. In previous versions of Ray, CPU detection in containers was incorrect. Please ensure that Ray has enough CPUs allocated. As a temporary workaround to revert to the prior behavior, set `RAY_USE_MULTIPROCESSING_CPU_COUNT=1` as an env var before starting Ray. Set the env var: `RAY_DISABLE_DOCKER_CPU_WARNING=1` to mute this warning.\n",
      "2023-01-31 15:06:27,468\tINFO worker.py:1509 -- Started a local Ray instance. View the dashboard at \u001B[1m\u001B[32mhttp://127.0.0.1:8265 \u001B[39m\u001B[22m\n",
      "2023-01-31 15:06:28,228\tWARNING function_trainable.py:619 -- Function checkpointing is disabled. This may result in unexpected behavior when using checkpointing features or certain schedulers. To enable, set the train function arguments to be `func(config, checkpoint_dir=None)`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "== Status ==<br>Current time: 2023-01-31 15:06:39 (running for 00:00:11.09)<br>Memory usage on this node: 48.3/251.6 GiB<br>Using FIFO scheduling algorithm.<br>Resources requested: 0/12 CPUs, 0/1 GPUs, 0.0/18.59 GiB heap, 0.0/9.3 GiB objects<br>Result logdir: /root/ray_results/train_model_2023-01-31_15-06-28<br>Number of trials: 20/20 (20 TERMINATED)<br><table>\n",
       "<thead>\n",
       "<tr><th>Trial name             </th><th>status    </th><th>loc             </th><th style=\"text-align: right;\">  linear1</th><th style=\"text-align: right;\">  linear2</th><th style=\"text-align: right;\">  iter</th><th style=\"text-align: right;\">  total time (s)</th><th style=\"text-align: right;\">  my_loss</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>train_model_c8362_00000</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        2</td><td style=\"text-align: right;\">        2</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.416409</td><td style=\"text-align: right;\"> 0.991657</td></tr>\n",
       "<tr><td>train_model_c8362_00001</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        2</td><td style=\"text-align: right;\">        8</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.341048</td><td style=\"text-align: right;\"> 0.983202</td></tr>\n",
       "<tr><td>train_model_c8362_00002</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">       16</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.339103</td><td style=\"text-align: right;\"> 0.976943</td></tr>\n",
       "<tr><td>train_model_c8362_00003</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        3</td><td style=\"text-align: right;\">       16</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.344286</td><td style=\"text-align: right;\"> 0.979999</td></tr>\n",
       "<tr><td>train_model_c8362_00004</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">       16</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.330642</td><td style=\"text-align: right;\"> 0.976378</td></tr>\n",
       "<tr><td>train_model_c8362_00005</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.328428</td><td style=\"text-align: right;\"> 0.986233</td></tr>\n",
       "<tr><td>train_model_c8362_00006</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">        2</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.335543</td><td style=\"text-align: right;\"> 0.978975</td></tr>\n",
       "<tr><td>train_model_c8362_00007</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        2</td><td style=\"text-align: right;\">        8</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.338412</td><td style=\"text-align: right;\"> 0.979499</td></tr>\n",
       "<tr><td>train_model_c8362_00008</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        3</td><td style=\"text-align: right;\">       16</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.32214 </td><td style=\"text-align: right;\"> 0.975357</td></tr>\n",
       "<tr><td>train_model_c8362_00009</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        3</td><td style=\"text-align: right;\">        2</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.324543</td><td style=\"text-align: right;\"> 0.978736</td></tr>\n",
       "<tr><td>train_model_c8362_00010</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        3</td><td style=\"text-align: right;\">        8</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.341047</td><td style=\"text-align: right;\"> 0.987305</td></tr>\n",
       "<tr><td>train_model_c8362_00011</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        3</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.340042</td><td style=\"text-align: right;\"> 0.990154</td></tr>\n",
       "<tr><td>train_model_c8362_00012</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        2</td><td style=\"text-align: right;\">        8</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.329659</td><td style=\"text-align: right;\"> 0.989246</td></tr>\n",
       "<tr><td>train_model_c8362_00013</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        2</td><td style=\"text-align: right;\">       16</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.340112</td><td style=\"text-align: right;\"> 0.97778 </td></tr>\n",
       "<tr><td>train_model_c8362_00014</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">       16</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.330477</td><td style=\"text-align: right;\"> 0.976697</td></tr>\n",
       "<tr><td>train_model_c8362_00015</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        3</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.335889</td><td style=\"text-align: right;\"> 0.983866</td></tr>\n",
       "<tr><td>train_model_c8362_00016</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.34328 </td><td style=\"text-align: right;\"> 0.982939</td></tr>\n",
       "<tr><td>train_model_c8362_00017</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.327036</td><td style=\"text-align: right;\"> 0.977793</td></tr>\n",
       "<tr><td>train_model_c8362_00018</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">       16</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.340566</td><td style=\"text-align: right;\"> 0.97617 </td></tr>\n",
       "<tr><td>train_model_c8362_00019</td><td>TERMINATED</td><td>172.17.0.11:3856</td><td style=\"text-align: right;\">        4</td><td style=\"text-align: right;\">       16</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">        0.33954 </td><td style=\"text-align: right;\"> 0.979511</td></tr>\n",
       "</tbody>\n",
       "</table><br><br>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Result for train_model_c8362_00000:\n",
      "  date: 2023-01-31_15-06-32\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9916567802429199\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.4164090156555176\n",
      "  time_this_iter_s: 0.4164090156555176\n",
      "  time_total_s: 0.4164090156555176\n",
      "  timestamp: 1675148792\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00000\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00000:\n",
      "  date: 2023-01-31_15-06-32\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 0_linear1=2,linear2=2\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9916567802429199\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.4164090156555176\n",
      "  time_this_iter_s: 0.4164090156555176\n",
      "  time_total_s: 0.4164090156555176\n",
      "  timestamp: 1675148792\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00000\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00001:\n",
      "  date: 2023-01-31_15-06-32\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9832018613815308\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3410482406616211\n",
      "  time_this_iter_s: 0.3410482406616211\n",
      "  time_total_s: 0.3410482406616211\n",
      "  timestamp: 1675148792\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00001\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00001:\n",
      "  date: 2023-01-31_15-06-32\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 1_linear1=2,linear2=8\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9832018613815308\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3410482406616211\n",
      "  time_this_iter_s: 0.3410482406616211\n",
      "  time_total_s: 0.3410482406616211\n",
      "  timestamp: 1675148792\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00001\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00002:\n",
      "  date: 2023-01-31_15-06-33\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9769430160522461\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.33910250663757324\n",
      "  time_this_iter_s: 0.33910250663757324\n",
      "  time_total_s: 0.33910250663757324\n",
      "  timestamp: 1675148793\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00002\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00002:\n",
      "  date: 2023-01-31_15-06-33\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 2_linear1=4,linear2=16\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9769430160522461\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.33910250663757324\n",
      "  time_this_iter_s: 0.33910250663757324\n",
      "  time_total_s: 0.33910250663757324\n",
      "  timestamp: 1675148793\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00002\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00003:\n",
      "  date: 2023-01-31_15-06-33\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9799988865852356\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3442862033843994\n",
      "  time_this_iter_s: 0.3442862033843994\n",
      "  time_total_s: 0.3442862033843994\n",
      "  timestamp: 1675148793\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00003\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00003:\n",
      "  date: 2023-01-31_15-06-33\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 3_linear1=3,linear2=16\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9799988865852356\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3442862033843994\n",
      "  time_this_iter_s: 0.3442862033843994\n",
      "  time_total_s: 0.3442862033843994\n",
      "  timestamp: 1675148793\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00003\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00004:\n",
      "  date: 2023-01-31_15-06-33\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9763780236244202\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3306424617767334\n",
      "  time_this_iter_s: 0.3306424617767334\n",
      "  time_total_s: 0.3306424617767334\n",
      "  timestamp: 1675148793\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00004\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00004:\n",
      "  date: 2023-01-31_15-06-33\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 4_linear1=4,linear2=16\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9763780236244202\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3306424617767334\n",
      "  time_this_iter_s: 0.3306424617767334\n",
      "  time_total_s: 0.3306424617767334\n",
      "  timestamp: 1675148793\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00004\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00005:\n",
      "  date: 2023-01-31_15-06-34\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9862332940101624\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3284280300140381\n",
      "  time_this_iter_s: 0.3284280300140381\n",
      "  time_total_s: 0.3284280300140381\n",
      "  timestamp: 1675148794\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00005\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00005:\n",
      "  date: 2023-01-31_15-06-34\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 5_linear1=4,linear2=4\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9862332940101624\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3284280300140381\n",
      "  time_this_iter_s: 0.3284280300140381\n",
      "  time_total_s: 0.3284280300140381\n",
      "  timestamp: 1675148794\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00005\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00006:\n",
      "  date: 2023-01-31_15-06-34\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9789753556251526\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3355426788330078\n",
      "  time_this_iter_s: 0.3355426788330078\n",
      "  time_total_s: 0.3355426788330078\n",
      "  timestamp: 1675148794\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00006\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00006:\n",
      "  date: 2023-01-31_15-06-34\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 6_linear1=4,linear2=2\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9789753556251526\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3355426788330078\n",
      "  time_this_iter_s: 0.3355426788330078\n",
      "  time_total_s: 0.3355426788330078\n",
      "  timestamp: 1675148794\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00006\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00007:\n",
      "  date: 2023-01-31_15-06-34\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.979499101638794\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.338411808013916\n",
      "  time_this_iter_s: 0.338411808013916\n",
      "  time_total_s: 0.338411808013916\n",
      "  timestamp: 1675148794\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00007\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00007:\n",
      "  date: 2023-01-31_15-06-34\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 7_linear1=2,linear2=8\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.979499101638794\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.338411808013916\n",
      "  time_this_iter_s: 0.338411808013916\n",
      "  time_total_s: 0.338411808013916\n",
      "  timestamp: 1675148794\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00007\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00008:\n",
      "  date: 2023-01-31_15-06-35\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9753565192222595\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3221395015716553\n",
      "  time_this_iter_s: 0.3221395015716553\n",
      "  time_total_s: 0.3221395015716553\n",
      "  timestamp: 1675148795\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00008\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00008:\n",
      "  date: 2023-01-31_15-06-35\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 8_linear1=3,linear2=16\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9753565192222595\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3221395015716553\n",
      "  time_this_iter_s: 0.3221395015716553\n",
      "  time_total_s: 0.3221395015716553\n",
      "  timestamp: 1675148795\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00008\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00009:\n",
      "  date: 2023-01-31_15-06-35\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9787362217903137\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3245425224304199\n",
      "  time_this_iter_s: 0.3245425224304199\n",
      "  time_total_s: 0.3245425224304199\n",
      "  timestamp: 1675148795\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00009\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00009:\n",
      "  date: 2023-01-31_15-06-35\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 9_linear1=3,linear2=2\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9787362217903137\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3245425224304199\n",
      "  time_this_iter_s: 0.3245425224304199\n",
      "  time_total_s: 0.3245425224304199\n",
      "  timestamp: 1675148795\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00009\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00010:\n",
      "  date: 2023-01-31_15-06-36\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9873045682907104\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3410465717315674\n",
      "  time_this_iter_s: 0.3410465717315674\n",
      "  time_total_s: 0.3410465717315674\n",
      "  timestamp: 1675148796\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00010\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00010:\n",
      "  date: 2023-01-31_15-06-36\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 10_linear1=3,linear2=8\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9873045682907104\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3410465717315674\n",
      "  time_this_iter_s: 0.3410465717315674\n",
      "  time_total_s: 0.3410465717315674\n",
      "  timestamp: 1675148796\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00010\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00011:\n",
      "  date: 2023-01-31_15-06-36\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9901538491249084\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3400423526763916\n",
      "  time_this_iter_s: 0.3400423526763916\n",
      "  time_total_s: 0.3400423526763916\n",
      "  timestamp: 1675148796\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00011\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00011:\n",
      "  date: 2023-01-31_15-06-36\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 11_linear1=3,linear2=4\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9901538491249084\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3400423526763916\n",
      "  time_this_iter_s: 0.3400423526763916\n",
      "  time_total_s: 0.3400423526763916\n",
      "  timestamp: 1675148796\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00011\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00012:\n",
      "  date: 2023-01-31_15-06-36\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9892460107803345\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.32965922355651855\n",
      "  time_this_iter_s: 0.32965922355651855\n",
      "  time_total_s: 0.32965922355651855\n",
      "  timestamp: 1675148796\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00012\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00012:\n",
      "  date: 2023-01-31_15-06-36\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 12_linear1=2,linear2=8\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9892460107803345\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.32965922355651855\n",
      "  time_this_iter_s: 0.32965922355651855\n",
      "  time_total_s: 0.32965922355651855\n",
      "  timestamp: 1675148796\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00012\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00013:\n",
      "  date: 2023-01-31_15-06-37\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.977779746055603\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.34011173248291016\n",
      "  time_this_iter_s: 0.34011173248291016\n",
      "  time_total_s: 0.34011173248291016\n",
      "  timestamp: 1675148797\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00013\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00013:\n",
      "  date: 2023-01-31_15-06-37\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 13_linear1=2,linear2=16\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.977779746055603\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.34011173248291016\n",
      "  time_this_iter_s: 0.34011173248291016\n",
      "  time_total_s: 0.34011173248291016\n",
      "  timestamp: 1675148797\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00013\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00014:\n",
      "  date: 2023-01-31_15-06-37\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9766972661018372\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.330477237701416\n",
      "  time_this_iter_s: 0.330477237701416\n",
      "  time_total_s: 0.330477237701416\n",
      "  timestamp: 1675148797\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00014\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00014:\n",
      "  date: 2023-01-31_15-06-37\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 14_linear1=4,linear2=16\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9766972661018372\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.330477237701416\n",
      "  time_this_iter_s: 0.330477237701416\n",
      "  time_total_s: 0.330477237701416\n",
      "  timestamp: 1675148797\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00014\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00015:\n",
      "  date: 2023-01-31_15-06-37\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9838660955429077\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3358888626098633\n",
      "  time_this_iter_s: 0.3358888626098633\n",
      "  time_total_s: 0.3358888626098633\n",
      "  timestamp: 1675148797\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00015\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00015:\n",
      "  date: 2023-01-31_15-06-37\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 15_linear1=3,linear2=4\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9838660955429077\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3358888626098633\n",
      "  time_this_iter_s: 0.3358888626098633\n",
      "  time_total_s: 0.3358888626098633\n",
      "  timestamp: 1675148797\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00015\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00016:\n",
      "  date: 2023-01-31_15-06-38\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9829390645027161\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3432800769805908\n",
      "  time_this_iter_s: 0.3432800769805908\n",
      "  time_total_s: 0.3432800769805908\n",
      "  timestamp: 1675148798\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00016\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00016:\n",
      "  date: 2023-01-31_15-06-38\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 16_linear1=4,linear2=4\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9829390645027161\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3432800769805908\n",
      "  time_this_iter_s: 0.3432800769805908\n",
      "  time_total_s: 0.3432800769805908\n",
      "  timestamp: 1675148798\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00016\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00017:\n",
      "  date: 2023-01-31_15-06-38\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9777934551239014\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.32703614234924316\n",
      "  time_this_iter_s: 0.32703614234924316\n",
      "  time_total_s: 0.32703614234924316\n",
      "  timestamp: 1675148798\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00017\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00017:\n",
      "  date: 2023-01-31_15-06-38\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 17_linear1=4,linear2=4\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9777934551239014\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.32703614234924316\n",
      "  time_this_iter_s: 0.32703614234924316\n",
      "  time_total_s: 0.32703614234924316\n",
      "  timestamp: 1675148798\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00017\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00018:\n",
      "  date: 2023-01-31_15-06-38\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9761696457862854\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.34056639671325684\n",
      "  time_this_iter_s: 0.34056639671325684\n",
      "  time_total_s: 0.34056639671325684\n",
      "  timestamp: 1675148798\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00018\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00018:\n",
      "  date: 2023-01-31_15-06-38\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 18_linear1=4,linear2=16\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9761696457862854\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.34056639671325684\n",
      "  time_this_iter_s: 0.34056639671325684\n",
      "  time_total_s: 0.34056639671325684\n",
      "  timestamp: 1675148798\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00018\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00019:\n",
      "  date: 2023-01-31_15-06-39\n",
      "  done: false\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9795113205909729\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3395400047302246\n",
      "  time_this_iter_s: 0.3395400047302246\n",
      "  time_total_s: 0.3395400047302246\n",
      "  timestamp: 1675148799\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00019\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n",
      "Result for train_model_c8362_00019:\n",
      "  date: 2023-01-31_15-06-39\n",
      "  done: true\n",
      "  experiment_id: 7c94a3a22418429b8386ce355f9e386d\n",
      "  experiment_tag: 19_linear1=4,linear2=16\n",
      "  hostname: autodl-container-095311853c-3000e9c7\n",
      "  iterations_since_restore: 1\n",
      "  my_loss: 0.9795113205909729\n",
      "  node_ip: 172.17.0.11\n",
      "  pid: 3856\n",
      "  time_since_restore: 0.3395400047302246\n",
      "  time_this_iter_s: 0.3395400047302246\n",
      "  time_total_s: 0.3395400047302246\n",
      "  timestamp: 1675148799\n",
      "  timesteps_since_restore: 0\n",
      "  training_iteration: 1\n",
      "  trial_id: c8362_00019\n",
      "  warmup_time: 0.004136085510253906\n",
      "  \n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-01-31 15:06:39,601\tINFO tune.py:758 -- Total run time: 11.37 seconds (11.07 seconds for the tuning loop).\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======================== Result =========================\n",
      "              my_loss  time_this_iter_s  done timesteps_total episodes_total  \\\n",
      "trial_id                                                                       \n",
      "c8362_00000  0.991657          0.416409  True            None           None   \n",
      "c8362_00001  0.983202          0.341048  True            None           None   \n",
      "c8362_00002  0.976943          0.339103  True            None           None   \n",
      "c8362_00003  0.979999          0.344286  True            None           None   \n",
      "c8362_00004  0.976378          0.330642  True            None           None   \n",
      "c8362_00005  0.986233          0.328428  True            None           None   \n",
      "c8362_00006  0.978975          0.335543  True            None           None   \n",
      "c8362_00007  0.979499          0.338412  True            None           None   \n",
      "c8362_00008  0.975357          0.322140  True            None           None   \n",
      "c8362_00009  0.978736          0.324543  True            None           None   \n",
      "c8362_00010  0.987305          0.341047  True            None           None   \n",
      "c8362_00011  0.990154          0.340042  True            None           None   \n",
      "c8362_00012  0.989246          0.329659  True            None           None   \n",
      "c8362_00013  0.977780          0.340112  True            None           None   \n",
      "c8362_00014  0.976697          0.330477  True            None           None   \n",
      "c8362_00015  0.983866          0.335889  True            None           None   \n",
      "c8362_00016  0.982939          0.343280  True            None           None   \n",
      "c8362_00017  0.977793          0.327036  True            None           None   \n",
      "c8362_00018  0.976170          0.340566  True            None           None   \n",
      "c8362_00019  0.979511          0.339540  True            None           None   \n",
      "\n",
      "             training_iteration                     experiment_id  \\\n",
      "trial_id                                                            \n",
      "c8362_00000                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00001                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00002                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00003                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00004                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00005                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00006                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00007                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00008                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00009                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00010                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00011                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00012                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00013                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00014                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00015                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00016                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00017                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00018                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "c8362_00019                   1  7c94a3a22418429b8386ce355f9e386d   \n",
      "\n",
      "                            date   timestamp  time_total_s   pid  \\\n",
      "trial_id                                                           \n",
      "c8362_00000  2023-01-31_15-06-32  1675148792      0.416409  3856   \n",
      "c8362_00001  2023-01-31_15-06-32  1675148792      0.341048  3856   \n",
      "c8362_00002  2023-01-31_15-06-33  1675148793      0.339103  3856   \n",
      "c8362_00003  2023-01-31_15-06-33  1675148793      0.344286  3856   \n",
      "c8362_00004  2023-01-31_15-06-33  1675148793      0.330642  3856   \n",
      "c8362_00005  2023-01-31_15-06-34  1675148794      0.328428  3856   \n",
      "c8362_00006  2023-01-31_15-06-34  1675148794      0.335543  3856   \n",
      "c8362_00007  2023-01-31_15-06-34  1675148794      0.338412  3856   \n",
      "c8362_00008  2023-01-31_15-06-35  1675148795      0.322140  3856   \n",
      "c8362_00009  2023-01-31_15-06-35  1675148795      0.324543  3856   \n",
      "c8362_00010  2023-01-31_15-06-36  1675148796      0.341047  3856   \n",
      "c8362_00011  2023-01-31_15-06-36  1675148796      0.340042  3856   \n",
      "c8362_00012  2023-01-31_15-06-36  1675148796      0.329659  3856   \n",
      "c8362_00013  2023-01-31_15-06-37  1675148797      0.340112  3856   \n",
      "c8362_00014  2023-01-31_15-06-37  1675148797      0.330477  3856   \n",
      "c8362_00015  2023-01-31_15-06-37  1675148797      0.335889  3856   \n",
      "c8362_00016  2023-01-31_15-06-38  1675148798      0.343280  3856   \n",
      "c8362_00017  2023-01-31_15-06-38  1675148798      0.327036  3856   \n",
      "c8362_00018  2023-01-31_15-06-38  1675148798      0.340566  3856   \n",
      "c8362_00019  2023-01-31_15-06-39  1675148799      0.339540  3856   \n",
      "\n",
      "                                         hostname      node_ip  \\\n",
      "trial_id                                                         \n",
      "c8362_00000  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00001  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00002  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00003  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00004  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00005  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00006  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00007  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00008  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00009  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00010  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00011  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00012  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00013  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00014  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00015  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00016  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00017  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00018  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "c8362_00019  autodl-container-095311853c-3000e9c7  172.17.0.11   \n",
      "\n",
      "             time_since_restore  timesteps_since_restore  \\\n",
      "trial_id                                                   \n",
      "c8362_00000            0.416409                        0   \n",
      "c8362_00001            0.341048                        0   \n",
      "c8362_00002            0.339103                        0   \n",
      "c8362_00003            0.344286                        0   \n",
      "c8362_00004            0.330642                        0   \n",
      "c8362_00005            0.328428                        0   \n",
      "c8362_00006            0.335543                        0   \n",
      "c8362_00007            0.338412                        0   \n",
      "c8362_00008            0.322140                        0   \n",
      "c8362_00009            0.324543                        0   \n",
      "c8362_00010            0.341047                        0   \n",
      "c8362_00011            0.340042                        0   \n",
      "c8362_00012            0.329659                        0   \n",
      "c8362_00013            0.340112                        0   \n",
      "c8362_00014            0.330477                        0   \n",
      "c8362_00015            0.335889                        0   \n",
      "c8362_00016            0.343280                        0   \n",
      "c8362_00017            0.327036                        0   \n",
      "c8362_00018            0.340566                        0   \n",
      "c8362_00019            0.339540                        0   \n",
      "\n",
      "             iterations_since_restore  warmup_time           experiment_tag  \\\n",
      "trial_id                                                                      \n",
      "c8362_00000                         1     0.004136    0_linear1=2,linear2=2   \n",
      "c8362_00001                         1     0.004136    1_linear1=2,linear2=8   \n",
      "c8362_00002                         1     0.004136   2_linear1=4,linear2=16   \n",
      "c8362_00003                         1     0.004136   3_linear1=3,linear2=16   \n",
      "c8362_00004                         1     0.004136   4_linear1=4,linear2=16   \n",
      "c8362_00005                         1     0.004136    5_linear1=4,linear2=4   \n",
      "c8362_00006                         1     0.004136    6_linear1=4,linear2=2   \n",
      "c8362_00007                         1     0.004136    7_linear1=2,linear2=8   \n",
      "c8362_00008                         1     0.004136   8_linear1=3,linear2=16   \n",
      "c8362_00009                         1     0.004136    9_linear1=3,linear2=2   \n",
      "c8362_00010                         1     0.004136   10_linear1=3,linear2=8   \n",
      "c8362_00011                         1     0.004136   11_linear1=3,linear2=4   \n",
      "c8362_00012                         1     0.004136   12_linear1=2,linear2=8   \n",
      "c8362_00013                         1     0.004136  13_linear1=2,linear2=16   \n",
      "c8362_00014                         1     0.004136  14_linear1=4,linear2=16   \n",
      "c8362_00015                         1     0.004136   15_linear1=3,linear2=4   \n",
      "c8362_00016                         1     0.004136   16_linear1=4,linear2=4   \n",
      "c8362_00017                         1     0.004136   17_linear1=4,linear2=4   \n",
      "c8362_00018                         1     0.004136  18_linear1=4,linear2=16   \n",
      "c8362_00019                         1     0.004136  19_linear1=4,linear2=16   \n",
      "\n",
      "             config/linear1  config/linear2  \n",
      "trial_id                                     \n",
      "c8362_00000               2               2  \n",
      "c8362_00001               2               8  \n",
      "c8362_00002               4              16  \n",
      "c8362_00003               3              16  \n",
      "c8362_00004               4              16  \n",
      "c8362_00005               4               4  \n",
      "c8362_00006               4               2  \n",
      "c8362_00007               2               8  \n",
      "c8362_00008               3              16  \n",
      "c8362_00009               3               2  \n",
      "c8362_00010               3               8  \n",
      "c8362_00011               3               4  \n",
      "c8362_00012               2               8  \n",
      "c8362_00013               2              16  \n",
      "c8362_00014               4              16  \n",
      "c8362_00015               3               4  \n",
      "c8362_00016               4               4  \n",
      "c8362_00017               4               4  \n",
      "c8362_00018               4              16  \n",
      "c8362_00019               4              16  \n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import numpy as np\n",
    "from ray import tune\n",
    "\n",
    "\n",
    "class LinearRegressionModel(nn.Module):\n",
    "    def __init__(self, input_shape, linear1, linear2, output_shape):\n",
    "        super(LinearRegressionModel, self).__init__()\n",
    "        self.linear1 = nn.Linear(input_shape, linear1)\n",
    "        self.linear2 = nn.Linear(linear1, linear2)\n",
    "        self.linear3 = nn.Linear(linear2, output_shape)\n",
    "\n",
    "    def forward(self, x):\n",
    "        l1 = self.linear1(x)\n",
    "        l2 = self.linear2(l1)\n",
    "        l3 = self.linear3(l2)\n",
    "        return l3\n",
    "\n",
    "\n",
    "def train_model(config):  # 修改1：修改参数，所有的参数都要借助config传递\n",
    "    # 指定参数与损失函数\n",
    "    model = LinearRegressionModel(x_train.shape[1], config['linear1'], config['linear2'], 1)\n",
    "    epochs = 1000  # 迭代1000次\n",
    "    learning_rate = 0.01  # 学习率\n",
    "    optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)  # 优化函数\n",
    "    criterion = nn.MSELoss()  # Loss使用MSE值，目标是使MSE最小\n",
    "\n",
    "    loss_list = []\n",
    "    for epoch in range(epochs):\n",
    "        epoch += 1\n",
    "        optimizer.zero_grad()  # 梯度清零\n",
    "        outputs = model(x_train)  # 前向传播\n",
    "        loss = criterion(outputs, y_train)  # 计算损失\n",
    "        loss.backward()  # 返向传播\n",
    "        loss_list.append(loss.detach().numpy())\n",
    "        optimizer.step()  # 更新权重参数\n",
    "    mean_loss = np.mean(loss_list)\n",
    "    tune.report(my_loss=mean_loss)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    x_train = torch.randn(100, 4)  # 生成100个4维的随机数，作为训练集的 X\n",
    "    y_train = torch.randn(100, 1)  # 作为训练集的label\n",
    "    # train_model(x_train, y_train, 32, 8) # 修改2：就不需要这样启动了，注释掉这一行\n",
    "\n",
    "    # 修改3：下面就是封装的方法\n",
    "    config = {\n",
    "        \"linear1\": tune.sample_from(lambda _: np.random.randint(2, 5)),  # 自定义采样\n",
    "        \"linear2\": tune.choice([2, 4, 8, 16]),  # 从给定值中随机选择\n",
    "    }\n",
    "    result = tune.run(  # 执行训练过程，执行到这里就会根据config自动调参了\n",
    "        train_model,  # 要训练的模型\n",
    "        resources_per_trial={\"cpu\": 8, },  # 指定训练资源\n",
    "        config=config,\n",
    "        num_samples=20,  # 迭代的次数\n",
    "    )\n",
    "    # 得到最后的结果\n",
    "    print(\"======================== Result =========================\")\n",
    "    print(result.results_df)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fb1afa9-c1f8-4f1c-98ed-3c19321853b9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
